package com.lin.cms.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lin.cms.criteria.ConfigAttrCriteria;
import com.lin.cms.dao.CmsConfigMapper;
import com.lin.cms.dao.ConfigAttrMapper;
import com.lin.cms.dao.SiteMapper;
import com.lin.cms.domain.CmsConfig;
import com.lin.cms.domain.ConfigAttr;
import com.lin.cms.domain.Site;
import com.lin.cms.domain.SiteCriteria;
import com.lin.cms.service.SiteService;
import com.lin.cms.util.Paging;

@Service
public class SiteServiceImpl implements SiteService {
	
	@Autowired(required=false)
	private SiteMapper siteMapper;
	@Autowired(required=false)
	private CmsConfigMapper cmsConfigMapper;
	@Autowired(required=false)
	private ConfigAttrMapper configAttrMapper;
	
	public Site selectByPrimaryKey(Integer siteId) {
		// TODO Auto-generated method stub
		return siteMapper.selectByPrimaryKey(siteId);
	}
	
	public int deleteByExample(SiteCriteria example) {
		// TODO Auto-generated method stub
		int actualRowsAffected = 0;
		
		List<Site> _oldSiteList = siteMapper.selectByExample(example);
		
		if (_oldSiteList.size() != 0) {
			actualRowsAffected = siteMapper.deleteByExample(example);
		}
		
		return actualRowsAffected;
	}

	
	public int deleteByPrimaryKey(Integer siteId) {
		// TODO Auto-generated method stub
		int actualRowsAffected = 0;
		
		Site _oldSite = siteMapper.selectByPrimaryKey(siteId);
		
		if (_oldSite != null) {
			actualRowsAffected = siteMapper.deleteByPrimaryKey(siteId);
		}
		
		return actualRowsAffected;
	}

	
	public int insert(Site record) {
		// TODO Auto-generated method stub
		return siteMapper.insert(record);
	}

	
	public int insertSelective(Site record) {
		// TODO Auto-generated method stub
		return siteMapper.insertSelective(record);
	}

	public List<Site> selectByExample(SiteCriteria example) {
		// TODO Auto-generated method stub
		return siteMapper.selectByExample(example);
	}

	public int updateByExampleSelective(Site record, SiteCriteria example) {
		// TODO Auto-generated method stub
		return siteMapper.updateByExampleSelective(record, example);
	}

	
	public int updateByExample(Site record, SiteCriteria example) {
		// TODO Auto-generated method stub
		return siteMapper.updateByExample(record, example);
	}

	
	public int updateByPrimaryKeySelective(Site record) {
		// TODO Auto-generated method stub
		return siteMapper.updateByPrimaryKeySelective(record);
	}

	
	public int updateByPrimaryKey(Site record) {
		// TODO Auto-generated method stub
		return siteMapper.updateByPrimaryKey(record);
	}
	
	public List<Site> selectByPage(SiteCriteria example) {
		// TODO Auto-generated method stub
		int totalResults = siteMapper.countByExample(example);
		Paging paging = example.getPaging();
		paging.setTotalResults(totalResults);
		
		return siteMapper.selectByPage(example,new RowBounds(paging.getFirstResult(),paging.getMaxResults()));
	}

	
	public Site selectByDomain(String domain) {
		// TODO Auto-generated method stub
		return siteMapper.selectByDomain(domain);
	}

	public List<Site> getListFromCache() {
		// TODO Auto-generated method stub
		return null;
	}

	
	public List<Site> getSiteByUserId(Integer userId) {
		// TODO Auto-generated method stub
		return siteMapper.getSiteByUserId(userId);
	}

	public CmsConfig selectByConfigId(Integer configId) {
		// TODO Auto-generated method stub
		CmsConfig cmsConfig=cmsConfigMapper.selectByPrimaryKey(configId);
		Map<String, String> attr=new HashMap<String, String>();
		List<ConfigAttr> configAttrList=configAttrMapper.selectByCriteria(new ConfigAttrCriteria(configId));
		for(ConfigAttr configAttr:configAttrList){
			attr.put(configAttr.getAttrName(),configAttr.getAttrValue());
		}
		cmsConfig.setAttr(attr);
		return cmsConfig;
	}
}
